//
//  ArticleItem.m
//  Inventory
//
//  Created by Marco Alanen on 2007-07-28.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "ArticleItem.h"

@implementation ArticleItem

-(id) init
{
	[super init];
	
	name = nil;
	article_id = nil;
	stock = nil;
	price = nil;
	/*[self setName:@"No name"];
	[self setID:@""];
	[self setStock:[NSNumber numberWithInt: 0]];
	[self setPrice:[NSNumber numberWithInt: 0]];*/
	changes = [[NSMutableArray alloc] init];
	
	return self;
}

//===================================================================
// Add changelog entry
//===================================================================
-(void) addChangeLogEntry:(ChangeAction)action value:(NSString *)value
{
	Change *change = [[Change alloc] init];
	[change setAction:action];
	[change setValue:value];
	[changes addObject:change];
}

//===================================================================
// Get/set name
//===================================================================
-(NSString *) name
{
	return name;
}

-(void) setName: (NSString *) newName
{
	if(name != nil)
	{
		if([name isEqualToString:newName])
		{
			return;
		}
	
		[self addChangeLogEntry:CHANGE_NAME value:newName];
		[name release];
	}

	[newName retain];
	name = newName;
}

//===================================================================
// Get/set article id
//===================================================================
-(NSString *) article_id
{
	return article_id;
}

-(void) setID: (NSString *) newID
{
	if(article_id != nil)
	{
		if([article_id isEqualToString:newID])
		{
			return;
		}
		
		[self addChangeLogEntry:CHANGE_ARTICLE_ID value:newID];
		[article_id release];
	}
	else

	[newID retain];
	article_id = newID;
}

//===================================================================
// Get/set stock
//===================================================================
-(NSNumber *) stock
{
	return stock;
}

-(void) setStock: (NSNumber *) newStock
{
	if(stock != nil)
	{
		if([stock isEqualToNumber:newStock])
		{
			return;
		}
		
		//[self addChangeLogEntry:CHANGE_STOCK value:
		[stock release];
	}
	
	[newStock retain];
	stock = newStock;
}

//===================================================================
// Get/set price
//===================================================================
-(NSNumber *) price
{
	return price;
}

-(void) setPrice: (NSNumber *) newPrice
{
	if(price != nil)
	{
		if([price isEqualToNumber:newPrice])
		{
			return;
		}
		
		[price release];
	}
	
	[newPrice retain];
	price = newPrice;
}

//===================================================================
// Comparison methods
//===================================================================
-(NSComparisonResult) compareName:(ArticleItem *)item
{
	return [name compare:[item name]];
}

-(NSComparisonResult) compareID:(ArticleItem *)item
{
	return [article_id compare:[item article_id]];
}

-(NSComparisonResult) compareStock:(ArticleItem *)item
{
	return [stock compare:[item stock]];
}

-(NSComparisonResult) comparePrice:(ArticleItem *)item
{
	return [price compare:[item price]];
}

//===================================================================
// Load item
//===================================================================
-(id) initWithCoder:(NSCoder *)coder
{
	[super init];
	
	[self setName:[coder decodeObject]];
	[self setID:[coder decodeObject]];
	[self setStock:[coder decodeObject]];
	[self setPrice:[coder decodeObject]];
	
	return self;
}

//===================================================================
// Save item
//===================================================================
-(void) encodeWithCoder:(NSCoder *)coder
{
	[coder encodeObject:[self name]];
	[coder encodeObject:[self article_id]];
	[coder encodeObject:[self stock]];
	[coder encodeObject:[self price]];
}

@end
